package com.redwine.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.redwine.app.R;
import com.redwine.app.net.responses.UploadImagesResponse;
import com.redwine.app.utils.BitmapUtils;
import com.redwine.app.view.SquareLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 修改界面展示图片的适配器
 * 作者：gassion on 17/9/12 00:12
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class SendShowImageAdapter extends BaseAdapter {

    private List<UploadImagesResponse.DataEntity> images = new ArrayList<>();

    private Context context;

    private AddImageListener addImagesListener;

    public SendShowImageAdapter(Context context) {
        this.context = context;
    }


    /**
     * 设置数据源
     */
    public void setData(List<UploadImagesResponse.DataEntity> images) {
        this.images = images;
        notifyDataSetChanged();
    }

    /**
     * 点击添加图片的回调函数
     *
     * @param addImagesListener
     */
    public void setAddImagesListener(AddImageListener addImagesListener) {
        this.addImagesListener = addImagesListener;
    }

    @Override
    public int getCount() {

        return images.size() < 9 ? (images.size() + 1) : images.size();
    }

    @Override
    public Object getItem(int position) {
        if (images.size() < 9) {
            return images.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.activity_send_show_image_item, parent, false);
            viewHolder = new ViewHolder();
            ViewUtils.inject(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (position > images.size() - 1) {
            viewHolder.ivDelete.setVisibility(View.GONE);
            viewHolder.ivAddPic.setVisibility(View.VISIBLE);
            viewHolder.ivShowPic.setVisibility(View.GONE);
            viewHolder.send_square_layout.setBackground(context.getResources().getDrawable(R.drawable.send_add_pic_shape_corner));
            viewHolder.send_square_layout.setClickable(true);
            viewHolder.send_square_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != addImagesListener)
                        addImagesListener.onClickListener();
                }
            });
        } else {
            UploadImagesResponse.DataEntity dataEntity = images.get(position);
            if (!TextUtils.isEmpty(dataEntity.getLocalPath()))
                BitmapUtils.loadImage(viewHolder.ivShowPic, dataEntity.getLocalPath(), BitmapUtils.ROUND);
            else if (!TextUtils.isEmpty(dataEntity.getReal_url())) {
                BitmapUtils.loadImage(viewHolder.ivShowPic, dataEntity.getReal_url(), BitmapUtils.ROUND);
            } else {
                BitmapUtils.loadImage(viewHolder.ivShowPic, dataEntity.getUrl(), BitmapUtils.ROUND);
            }
            viewHolder.ivAddPic.setVisibility(View.GONE);
            viewHolder.ivShowPic.setVisibility(View.VISIBLE);
            viewHolder.send_square_layout.setBackground(null);
            viewHolder.ivDelete.setVisibility(View.VISIBLE);
            viewHolder.send_square_layout.setClickable(false);
            viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    images.remove(position);
                    notifyDataSetChanged();
                }
            });
        }
        return convertView;
    }

    public interface AddImageListener {
        void onClickListener();
    }

    public class ViewHolder {
        @ViewInject(R.id.send_item_iv_pic)
        public ImageView ivShowPic;
        @ViewInject(R.id.send_item_iv_add)
        public ImageView ivAddPic;
        @ViewInject(R.id.send_item_iv_delete)
        public ImageView ivDelete;
        @ViewInject(R.id.send_square_layout)
        public SquareLayout send_square_layout;
    }
}
